#ifndef _MYCONFIG_H_
#define _MYCONFIG_H_

#include <avr/io.h>
#include <string.h>
#include <util/delay.h>
#include <util/atomic.h>

/*-------------------------------------------------------*/
// Mot so macro
/*-------------------------------------------------------*/
typedef unsigned char   byte;           // 8-bit
typedef unsigned int    word;           // 16-bit
typedef unsigned long   dword;          // 32-bit

enum {false, true} bool;
enum bool {no, yes};

// Code compatibility to new AVR-libc
// outb(), inb(), inw(), outw(), BV(), sbi(), cbi(), sei(), cli()
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
#ifndef cbi
#define cbi(reg, bit) reg &= ~(_BV(bit))
#endif
#ifndef sbi
#define sbi(reg, bit) reg |= (_BV(bit))
#endif
#ifndef cli
#define cli() __asm__ __volatile__ ("cli" ::)
#endif
#ifndef sei
#define sei() __asm__ __volatile__ ("sei" ::)
#endif
#ifndef nop
#define nop() __asm__ __volatile__ ("nop" ::)
#endif

#define MIN(a, b)			(((a) < (b)) ? (a) : (b))
#define MAX(a, b)			(((a) > (b)) ? (a) : (b))
#define MINMAX(x, min, max)	(MIN(MAX((x), (min)), (max)))
#define INTMINMAX(x)		(MINMAX(x, -0x7FFF, 0x7FFF))
#define CONSTRAIN(x, a)		(MINMAX(x, -(a), (a)))

/*-------------------------------------------------------*/
// Cac he so tinh toan
/*-------------------------------------------------------*/

#define _180_DIV_PI_	57.295779513f	// 1rad

/*-------------------------------------------------------*/
// Cac cong
/*-------------------------------------------------------*/

#define PORT_LED	PORTB
#define DDR_LED		DDRB
#define LED			5

#define PORT_SETUP	PORTD
#define DDR_SETUP	DDRD
#define PIN_SETUP	PIND
#define PSETUP		7

#define PORT_SDA	PORTC
#define PORT_SCL	PORTC
#define DDR_SDA		DDRC
#define DDR_SCL		DDRC
#define SDA	4
#define SCL	5

#define PORT_RX		PORTD
#define PORT_TX		PORTD
#define DDR_RX		PIND
#define DDR_TX		PIND
#define RX			0
#define TX			1

#define RXB8		1
#define TXB8		0
#define UPE			2
#define OVR			3
#define FE			4
#define UDRE		5
#define RXC			7

#define FRAMING_ERROR		(1<<FE)
#define PARITY_ERROR		(1<<UPE)
#define DATA_OVERRUN		(1<<OVR)
#define DATA_REGISTER_EMPTY	(1<<UDRE)
#define RX_COMPLETE			(1<<RXC)

#define	PORT_OUT	PORTC
#define	DDR_OUT		DDRC
#define	C0			0	// front
#define	C1			1	// back
#define	C2			2	// left
#define	C3			3	// right

#define	PORT_IN		PORTD
#define	DDR_IN		DDRD
#define	D2			2	// INT0 ~ PPM
#define	D3			3
#define	D4			4
#define	D5			5
#define	D6			6
#define	D7			7

/*-------------------------------------------------------*/
// Cau hinh cac bo dieu khien
/*-------------------------------------------------------*/

extern unsigned char setup_status;

// USART Receiver buffer for UART transmit
#define RX_BUFFER_SIZE0 255
unsigned char rx_buffer0[RX_BUFFER_SIZE0];

#if RX_BUFFER_SIZE0 <= 256
unsigned char rx_wr_index0, rx_rd_index0, rx_counter0;
#else
unsigned int rx_wr_index0, rx_rd_index0, rx_counter0;
#endif

//unsigned char rx_buffer_overflow0;

// dat gia tri khoi tao TCNT0 cua TIMER0
#define TCNT0_SET_1000MICROSEC (unsigned char)(256 - (1000 * (1 / 64) * (F_CPU / 1000000L)))
//                                              |       |         |____ PreScale
//                                              |       |______________ microsec
//                                              |______________________ 8 bit TIMER0
//										

// TIMER1
#define ESC_FREQ		50	// Hz
#define NUM_SERVO		4
#define NUM_CHANEL		6
#define PWM_PRESCALER	8
#define TOTAL_OCR1A		F_CPU / PWM_PRESCALER / ESC_FREQ	// tong so lan tang OCR1A trong 1 chu ky

#define TCNT1A_MIN		2000//F_CPU * 0.001 / PWM_PRESCALER
#define TCNT1A_MAX		4000//F_CPU * 0.002 / PWM_PRESCALER
#define TCNT1A_BASE		8000//NUM_SERVO * TCNT1A_MIN
#define OCR1A_REMAIN	12000//TOTAL_OCR1A - TCNT1A_BASE

#define TCNT1_US_TIME	PWM_PRESCALER * 1000000 / F_CPU		// thoi gian tang TCNT1 len 1 tinh theo us
#define PPW_FREQUENCY	50									// tan so 1 bo xung du lieu dieu khien
#define TCNT1_REMAIN	1000000 / 4 / PPW_FREQUENCY / TCNT1_US_TIME	// so lan dem TCNT1 ngoai vung gia tri xung d/k
//								  |___________________________ xung thua khoang 1/4 chu ky 50Hz
/*-------------------------------------------------------*/
// Funtions in MyConfig
/*-------------------------------------------------------*/
extern volatile unsigned long timer0_millis;

void MyMCU_TestSetup();
void MyMCU_Init();
void MyOutput_Init();
void MyInput_Init();
void MyI2C_Init();
void MyUSART_Init();
void MyTimer0_Init();
void MyTimer1_Init();
//void MyTimer1_Init_Setup_Mode();
//void MyCapture_Init();
void MyINT0_Init();
unsigned long millis();
unsigned long micros();
void SerialPrint(unsigned char c);
void SerialPrintByte(unsigned char b);		// print 1 byte
void SerialFloatPrint(float f);
void SerialIntPrint(int v);
void SerialInt32Print(int32_t f);
void SerialPrintFloatArr(float * arr, unsigned char length);
void SerialPrintIntArr(int * arr, unsigned char length);
float _8Bytes_To_Float(unsigned char *a);
int _4Bytes_To_Int(unsigned char *a);
unsigned char _2Bytes_To_Byte(unsigned char *a);

//float FourBytes_To_Float(unsigned char *_4bytes);

#endif
